﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ArbolandiaBL;
using Ext.Net;
using System.IO;

public partial class catalogTipsJardin : System.Web.UI.Page
{
    protected void Page_PreLoad(object sender, EventArgs e)
    {

        if (!ArbolandiaBL.Security.ValidLogin())
        {
            X.AddScript("this.parent.LogOut();");
        }

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!X.IsAjaxRequest)
        {
            ReloadGrid();
        }
    }
    protected void ReloadGrid()
    {
        try
        {
            this.storeContents.DataSource = new Service().GetAllTipsJardin();
            this.storeContents.DataBind();

        }
        catch (Exception ex)
        {
            //unexpected error
            X.Msg.Show(new MessageBoxConfig { Title = "Error", Message = ex.Message, Buttons = MessageBox.Button.OK, Icon = MessageBox.Icon.ERROR });
        }
    }
    protected void btnDelete_Click(object sender, DirectEventArgs e)
    {
        int id = 0;
        string json = e.ExtraParams["idValues"];

        if (string.IsNullOrEmpty(json))
        {
            X.Msg.Show(new MessageBoxConfig
            {
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.WARNING,
                Title = "Alerta",
                Message = "Favor de Seleccionar un registro"
            });
            return;
        }
        Dictionary<string, string>[] valuesdic = JSON.Deserialize<Dictionary<string, string>[]>(json);
        string sid = "";
        foreach (Dictionary<string, string> row in valuesdic)
        {
            sid = row["Id"];
        }

        Int32.TryParse(sid, out id);
        if (id <= 0)
        {
            X.Msg.Show(new MessageBoxConfig
            {
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.WARNING,
                Title = "Alerta",
                Message = "Favor de Seleccionar un registro"
            });
            return;
        }

        new Service().DeleteTipJardin(id);
        pnlTipsJardinCatalog.Reset();
        ReloadGrid();
        Ext.Net.Notification.Show(new Ext.Net.NotificationConfig
        {
            Title = Constants.TITLE_DELETE,
            Icon = Ext.Net.Icon.PageSave,
            AutoHide = true,
            Html = Constants.NOTIFY_DELETED
        });

    }
    protected void AddClick(object sender, DirectEventArgs e)
    {
        Arb_TipsJardin tip = new Arb_TipsJardin();
        tip.Category = txtCategory.Text;
        tip.Content = htmlContent.Text;
        tip.Title = txtTitle.Text;

        new Service().SaveTipJardin(tip);
        pnlTipsJardinCatalog.Reset();
        ReloadGrid();
        Ext.Net.Notification.Show(new Ext.Net.NotificationConfig
        {
            Title = Constants.TITLE_SAVE,
            Icon = Ext.Net.Icon.PageSave,
            AutoHide = true,
            Html = Constants.NOTIFY_SAVED
        });
    }
    protected void Store1_RefreshData(object sender, StoreReadDataEventArgs e)
    {
        ReloadGrid();
    }
}